//
//  DetailforActSecondVC.m
//  XDCommonApp
//
//  Created by xindaoapp on 14-3-10.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "DetailforActSecondVC.h"
#import "XDTools.h"
#import "XDHeader.h"

#define dHeightForHeader 36.0

@interface DetailforActSecondVC ()

{
    NSString *activityDate;
    NSString *activityTime;
    NSString *programTitle;
    NSString *programContent;
}


@end

@implementation DetailforActSecondVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
//    [self initWidget];
    [self getXmlData];
}

- (void)initWidget
{
    self.titleLabel.text = @"活动详情";
    self.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    self.titleLabel.textColor = TEXTBLACKCOLORE;
    
    self.detailActTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, UI_SCREEN_WIDTH,self.contentView.frame.size.height) style:UITableViewStylePlain];
    _detailActTableView.delegate = self;
    _detailActTableView.dataSource = self;
    _detailActTableView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_detailActTableView];
}

#pragma mark  --------------tableView delegate----------------

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return dHeightForHeader;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//    NSArray * array;
    CGFloat headerHeight=0;
    if (tableView == _detailActTableView){
        
//        array=@[@"2014年2月22日 星期一",@"2012年2月24日 星期三"];
        headerHeight = dHeightForHeader;
    }
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, headerHeight)];
    view.backgroundColor = RGBA(217, 217, 217, 1);
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, UI_SCREEN_WIDTH-15, headerHeight)];
    label.textColor = TEXTBLACKCOLORE;
    label.font = [UIFont systemFontOfSize:15];
    label.backgroundColor = [UIColor clearColor];
//    label.text = [array objectAtIndex:section];
    label.text = activityDate;
    
    [view addSubview:label];
    return view;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    return 150;
    return 120;
}


-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView == _detailActTableView){
        static NSString * cellName = @"cell";
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellName];
        if (cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
            cell.backgroundColor = [UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 290, 20)];
        timeLabel.backgroundColor = [UIColor clearColor];
        if (indexPath.section==0) {
            timeLabel.text = [NSString stringWithFormat:@"时间: %@",activityTime];//@"时间: 2014/02/22-2014/04/18";
        }else{
            timeLabel.text = @"时间: 2014/02/24-2014/03/18";
        }
        timeLabel.font = [UIFont systemFontOfSize:13];
        timeLabel.textColor = TEXTBLACKCOLORE;
        [cell.contentView addSubview:timeLabel];
        
        actTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(timeLabel.frame.origin.x, height_y(timeLabel), timeLabel.frame.size.width, 40)];
        actTitleLabel.backgroundColor = [UIColor clearColor];
        actTitleLabel.font = [UIFont boldSystemFontOfSize:15];
        actTitleLabel.numberOfLines = 2;
        actTitleLabel.lineBreakMode = NSLineBreakByCharWrapping;
        
        actTitleLabel.text = programTitle;//@"BEING ASIA——中日韩艺术家推荐展";
        actTitleLabel.textColor = TEXTBLACKCOLORE;
        [cell.contentView addSubview:actTitleLabel];
        
        UILabel *posLb = [[UILabel alloc ]initWithFrame:CGRectMake(actTitleLabel.frame.origin.x, height_y(actTitleLabel), 45, 25)];
        posLb.backgroundColor = [UIColor clearColor];
        posLb.text = @"内容:";
        posLb.textColor = TEXTBLACKCOLORE4;
        posLb.font = timeLabel.font;
        [cell.contentView addSubview:posLb];
        
        hostLabel = [[UILabel alloc]initWithFrame:CGRectMake(width_x(posLb), posLb.frame.origin.y+5, 245, 20)];
        hostLabel.backgroundColor = [UIColor clearColor];
        hostLabel.numberOfLines = 0;
        hostLabel.textColor = TEXTBLACKCOLORE4;
        hostLabel.font = timeLabel.font;
        
        CGSize Psize = CGSizeMake(140, 2000.0f);
        NSString *Pstr = @"蔡萌（雅昌摄影网艺术总监）\n毛卫东（三影堂出版总监）";//programContent;//
        Psize = [Pstr sizeWithFont:posLb.font constrainedToSize:CGSizeMake(hostLabel.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
        
        NSAttributedString *attributedString =[[NSAttributedString alloc] initWithString:Pstr attributes:@{NSKernAttributeName : @(5.0f)}];
        [hostLabel setAttributedText:attributedString];
        hostLabel.text = Pstr;
        //根据计算结果重新设置label的尺寸
        [hostLabel setFrame:CGRectMake(width_x(posLb), posLb.frame.origin.y+5, 230, Psize.height)];
        [cell.contentView addSubview:hostLabel];
        
        DDLOG(@"height_y(hostLabel) = %f",height_y(hostLabel));
        specialMediaLabel = [[UILabel alloc]initWithFrame:CGRectMake(posLb.frame.origin.x, height_y(hostLabel), 290, 25)];
        specialMediaLabel.backgroundColor = [UIColor clearColor];
        specialMediaLabel.numberOfLines = 0;
        specialMediaLabel.textColor = TEXTBLACKCOLORE4;
        specialMediaLabel.font = timeLabel.font;
        specialMediaLabel.text = @"特别支持媒体:《顶层》杂志";
//        [cell.contentView addSubview:specialMediaLabel];
        
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   
}

#pragma mark  -------- DataSouceManager------------

- (void)getXmlData
{
//       NSString *id_Str = _idStr;
//        NSString *type_Str = _typeStr;
//    _idStr = @"2";
//    _typeStr = @"2";
    
    if ([XDTools NetworkReachable]){
        NSString *body = [NSString stringWithFormat:@" <ActivityDetailById xmlns=\"http://tempuri.org/\">"
                          "<id>%@</id>"
                          "<type>%@</type>"
                          "</ActivityDetailById>",_idStr,_typeStr];
        
        __weak ASIFormDataRequest *request = [XDTools postRequestWithDict:body API:ACTIVITYDETAILBYID];
        [request setCompletionBlock:^{
            [XDTools hideProgress:self.contentView];
            //[XDTools hideProgress:self.contentView];
            NSString *responseString = [request responseString];
            // NSDictionary *tempDic = [XDTools  JSonFromString:responseString];
            DDLOG(@"responseString:%@", responseString);
            
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            int statusCode = [request responseStatusCode];
            NSString *soapAction = [[request requestHeaders] objectForKey:@"SOAPAction"];
            
            NSArray *arraySOAP =[soapAction componentsSeparatedByString:@"/"];
            int count = [arraySOAP count] - 1;
            NSString *methodName = [arraySOAP objectAtIndex:count];
            
            // Use when fetching text data
            NSString *result = nil;
            if (statusCode == 200) {
                //表示正常请求
                result = [SoapXmlParseHelper SoapMessageResultXml:responseString ServiceMethodName:methodName];
                NSDictionary  *responseDict = [XDTools JSonFromString:result];
                if ([[responseDict objectForKey:@"code"] intValue]==0){
                    DDLOG(@"res = %@",responseDict);
                    
                    activityDate = [NSString stringWithFormat:@"%@",[responseDict valueForKey:@"activityDate"]];
                   
                    if ([activityDate isEqualToString:@"<null>"]||[activityDate isKindOfClass:[NSNull class]]) {
                        activityDate = @"";
                    }
                    activityTime = [NSString stringWithFormat:@"%@",[responseDict valueForKey:@"activityTime"]];
                    if ([activityTime isEqualToString:@"<null>"]||[activityTime isKindOfClass:[NSNull class]]) {
                        activityTime = @"";
                    }
                    
                    programTitle = [NSString stringWithFormat:@"%@",[responseDict valueForKey:@"programTitle"]];
                    if ([programTitle isEqualToString:@"<null>"]||[programTitle isKindOfClass:[NSNull class]]) {
                        programTitle = @"";
                    }
                    
                    programContent = [NSString stringWithFormat:@"%@",[responseDict valueForKey:@"programContent"]];
                   
                    if ([programContent isEqualToString:@"<null>"]||[programContent isKindOfClass:[NSNull class]]) {
                        programContent = @"";
                    }
                    
//                    activityTitle = [responseDict valueForKey:@"activityTitle"];
//                    activityTime = [responseDict valueForKey:@"activityTime"];
//                    artNameStr = [responseDict valueForKey:@"activityArtName"];
//                    activitySponsor = [responseDict valueForKey:@"activitySponsor"];
//                    activeDesign = [responseDict valueForKey:@"activityDesign"];
//                    activityPosition = [responseDict valueForKey:@"activityPosition"];
//                    actArray = [responseDict valueForKey:@"activityOrganizationList"];
//                    activityIntroduce = [responseDict valueForKey:@"activityIntroduce"];
                    
                    [self initWidget];
//                    [_detailActTableView reloadData];
                    
                    
                }
            }
        }];
        
        [request setFailedBlock:^{
            [XDTools hideProgress:self.contentView];
        }];
        
        [request startAsynchronous];
        [XDTools showProgress:self.contentView];
    }else{
        [XDTools showTips:brokenNetwork toView:self.contentView];
    }
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
